<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      div {
        width: 300px;
        height: 300px;
        border: 1px solid #bfa;
      }
    </style>
  </head>
  <body>
    <button>cors</button>
    <div></div>
    <script>
      var btn = document.querySelector('button')
      var div = document.querySelector('div')
      btn.onclick = function () {
        var xhr = new XMLHttpRequest()
        // 设置返回的数据格式
        xhr.responseType = 'json'

        xhr.open('post', 'http://127.0.0.1:9527/cors')
        // 设置请求头(注意位置再open和send中间)
        // application/x-www-form-urlencoded 编码格式  参数的格式为key=value&key=value
        xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
        // 自定义设置请求头
        xhr.setRequestHeader('name', 'zsls')
        xhr.send('a=100&b=200&c=300') // post请求体
        xhr.onreadystatechange = function () {
          if (xhr.readyState === 4) {
            if (xhr.status >= 200 && xhr.status < 300) {
              div.innerHTML = xhr.response.data.name
            }
          }
        }
      }
    </script>
  </body>
</html>
